//
//  BNDOfficeDocVc.m
//  OfficeDoc
//
//  Created by pie on 2022/3/31.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocVc.h"
#import "BNDOfficeDocPopView.h"
#import "BNDEnterpriseShowView.h"
#import "BNDChooseCompanyView.h"
#import "BNDOfficeDocSearchView.h"
#import "BNDOfficeDocClassifyView.h"
#import "BNDOfficeDocTableView.h"
#import "LKNavBarTitleMenu.h"
#import "BNDOfficeDocSearchTipView.h"
#import "BNDOfficeDocHandle.h"
#import "BNDOfficeDocHelper.h"
#import "UIImage+YYAdd.h"
#import "LVRefreshFooter.h"
#import "LVHUD.h"

#import "BNDClassifyModel.h"

@interface BNDOfficeDocVc ()
@property (strong, nonatomic) BNDEnterpriseShowView *enterpriseShowView;
@property (strong, nonatomic) BNDChooseCompanyView *chooseCompanyView;
@property (strong, nonatomic) BNDOfficeDocSearchView *searchView;
@property (strong, nonatomic) BNDOfficeDocClassifyView *classifyView;
@property (strong, nonatomic) BNDOfficeDocSearchTipView *searchTipView;
@property (strong, nonatomic) BNDOfficeDocTableView *searchListView;
@property (strong, nonatomic) UIScrollView *listScrollView;
@property (weak  , nonatomic) NSArray<BNDClassifyModel *> * moreClass;
@property (strong, nonatomic) NSMutableArray *officeDocList;

@end

@implementation BNDOfficeDocVc

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"公告";
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xFFFFFF)];
        backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
        appearance.backgroundImage = backGroundImage;
        appearance.shadowColor = [UIColor clearColor];
        appearance.shadowImage = [UIImage new];
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;
    } else {
        UIImage *backGroundImage = [UIImage imageWithColor:UIColorWithHex(0xFFFFFF)];
        backGroundImage = [backGroundImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
        [self.navigationController.navigationBar setBackgroundImage:backGroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    
//    [BNDOfficeDocHandle bndRegisterInfo:@"00ptbnd1908018713159904060714715" openId:@"00ptbnd1962460793159946082614215" token:@"1"];
    
    if([BNDPreference sharedInstance].companyOpenId.length>0&&[BNDPreference sharedInstance].token.length>0&&[BNDPreference sharedInstance].openId.length>0){
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先初始化BNDOfficeDocHandlend的bndRegisterInfo方法" preferredStyle:UIAlertControllerStyleAlert];
        @weakify(self)
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self)
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:confirm];
        [self presentViewController:alert animated:true completion:nil];
        return;
    }
    
    [self.view addSubview:self.enterpriseShowView];
    [self.view addSubview:self.searchView];
    [self.view addSubview:self.searchTipView];
    [self.view addSubview:self.searchListView];

    [LVHUD showLoding:self.view];
    @weakify(self)
    [BNDApiManager officeDocRequestClassList:^(NSArray<BNDClassifyModel *> * _Nonnull moreClass) {
        @strongify(self)
        [LVHUD dismissLoding:self.view];
        self.moreClass = moreClass;
        [self handleLoadView];
    } failure:^(NSError * _Nullable error) {
        @strongify(self)
        [LVHUD dismissLoding:self.view];
        BNDClassifyModel *model = [BNDClassifyModel new];
        model.name = @"全部";
        model.classifyId = @"";
        NSMutableArray *array = [NSMutableArray arrayWithObject:model];
        self.moreClass = array;
        [self handleLoadView];
    }];
}

- (void)handleLoadView
{
    [self.view addSubview:self.classifyView];
    [self.view addSubview:self.listScrollView];
   [self handlePopWindos];
    NSMutableArray *array = [NSMutableArray array];
    @weakify(self)
    [self.moreClass enumerateObjectsUsingBlock:^(BNDClassifyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self)
        [array addObject:obj.name];
        BNDOfficeDocTableView *liveListView = [[BNDOfficeDocTableView alloc] initWithFrame:CGRectMake(kDeviceWidth*idx, 0, kDeviceWidth, self.listScrollView.height)];
        liveListView.classifyId = obj.classifyId;
        [self.listScrollView addSubview:liveListView];
    }];
    
    _classifyView.navBarTitleMenu.titles = [NSMutableArray arrayWithArray:array];
    _classifyView.navBarTitleMenu.contentScrollView = self.listScrollView;
    [[UIApplication sharedApplication].delegate.window addSubview:self.chooseCompanyView];
}


- (BNDOfficeDocTableView *)searchListView
{
    if (!_searchListView) {
        _searchListView = [[BNDOfficeDocTableView alloc] initWithFrame:CGRectMake(0,self.searchTipView.height+self.searchTipView.origin.y, kDeviceWidth, kDeviceHeight- self.searchTipView.height-self.searchTipView.origin.y)];
        _searchListView.hidden = YES;
        _searchListView.tableView.mj_footer = nil;
        @weakify(self)
        [_searchListView.helper setSearchCountBlock:^(id _Nullable count) {
            @strongify(self)
            self.searchTipView.searchCountText = count;
        }];
    }
    return _searchListView;
}

#pragma mark - 处理公告弹窗提示

- (void)handlePopWindos
{
    @weakify(self)
    [BNDApiManager officeDocRequestPopWindos:^(NSArray<BNDOfficeDocModel *> * _Nonnull moreClass) {
        @strongify(self)
        if(moreClass == nil){
            moreClass = [NSArray array];
        }
        self.officeDocList = [NSMutableArray arrayWithArray:moreClass];
        [self handleShowPopOfficDoc];
    } failure:^(NSError * _Nullable error) {
    }];
}

- (void)handleShowPopOfficDoc
{
    if(_officeDocList.count >0){
        BNDOfficeDocModel *model = _officeDocList[0];
        @weakify(self)
        BNDOfficeDocPopView *popView = [[BNDOfficeDocPopView alloc] initWithTitle:model clickAction:^(id _Nullable data) {
//            [self.officeDocList removeObjectAtIndex:0];
            [BNDPushVCHandler pushOfficeDocDetailsVc:data action:^{
                
            }];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self handleShowPopOfficDoc];
//            });
            
        } cancleAction:^(id _Nullable data) {
            
            @strongify(self)
            [self.officeDocList removeObjectAtIndex:0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self handleShowPopOfficDoc];
            });
        }];
        [popView show:self.view];
    }
}

#pragma mark - GET

- (BNDOfficeDocSearchTipView *)searchTipView
{
    if (!_searchTipView) {
        _searchTipView = [[BNDOfficeDocSearchTipView alloc] initWithFrame:CGRectMake(0,self.searchView.height+self.searchView.origin.y, kDeviceWidth, 44)];
        _searchTipView.hidden = YES;
    }
    return _searchTipView;
}

- (BNDChooseCompanyView *)chooseCompanyView
{
    if (!_chooseCompanyView) {
        _chooseCompanyView = [[BNDChooseCompanyView alloc] initWithFrame:CGRectMake(0,0, kDeviceWidth, kDeviceHeight)];
        @weakify(self)
        _chooseCompanyView.helperBlock = ^(id _Nullable data) {
            @strongify(self)
            BNDChooseCompanyModel *model = data;
            self.enterpriseShowView.model = model;
        };
        
    }
    return _chooseCompanyView;
}

- (BNDEnterpriseShowView *)enterpriseShowView
{
    if (!_enterpriseShowView) {
        _enterpriseShowView = [[BNDEnterpriseShowView alloc] initWithFrame:CGRectMake(0,BNDNavigationBarHeight(), kDeviceWidth, 54)];
        @weakify(self)
        [_enterpriseShowView setBlock:^(id _Nullable data) {
            @strongify(self)
            [self.chooseCompanyView showContentView];
        }];
    }
    return _enterpriseShowView;
}

- (BNDOfficeDocSearchView *)searchView
{
    if (!_searchView) {
        _searchView = [[BNDOfficeDocSearchView alloc] initWithFrame:CGRectMake(0,self.enterpriseShowView.height+self.enterpriseShowView.origin.y, kDeviceWidth, 54)];
        @weakify(self)
        [_searchView setSearchBlock:^(id _Nullable data) {
            @strongify(self)
            NSString *text = data;
            self.searchListView.helper.keyWord = text;
            [self.searchListView.helper reload];
            if(text.length ==0){
                self.classifyView.hidden = NO;
                self.listScrollView.hidden = NO;
                self.searchTipView.hidden = YES;
                self.searchListView.hidden = YES;
                self.searchTipView.searchCountText = @"0";
            }else{
                self.classifyView.hidden = YES;
                self.listScrollView.hidden = YES;
                self.searchTipView.hidden = NO;
                self.searchListView.hidden = NO;
            }
            
        }];
    }
    return _searchView;
}

- (BNDOfficeDocClassifyView *)classifyView
{
    if (!_classifyView) {
        _classifyView = [[BNDOfficeDocClassifyView alloc] initWithFrame:CGRectMake(0,self.searchView.height+self.searchView.origin.y, kDeviceWidth, 54)];
    }
    return _classifyView;
}


- (UIScrollView *)listScrollView
{
    if (!_listScrollView) {
        _listScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.classifyView.height+self.classifyView.origin.y, kDeviceWidth, kDeviceHeight- self.classifyView.height-self.classifyView.origin.y)];
        _listScrollView.backgroundColor = UIColor.whiteColor;
        [_listScrollView setScrollsToTop:NO];
        [_listScrollView setPagingEnabled:YES];
        [_listScrollView setShowsHorizontalScrollIndicator:NO];
        [_listScrollView setContentSize:(CGSize){self.view.width*4, kDeviceHeight- self.classifyView.height-self.classifyView.origin.y}];
    }
    return _listScrollView;
}
@end
